package pchi.pchicamera.data;

import java.io.File;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;

import pchi.pchicamera.helpers.BitmapHelpers;
import pchi.pchicamera.helpers.ExternalStorageHelpers;
import pchi.pchicamera.models.PhotoModel;

public class PhotosData {

	private ArrayList<PhotoModel> photos;

	private Boolean isLoaded;

	public PhotosData() {
		this.photos = new ArrayList<PhotoModel>();
	}

	public boolean isLoaded() {
		return isLoaded;
	}

	public void load(Context context) {
		this.photos.clear();

		this.isLoaded = false;

		String[] projection = new String[] { MediaStore.Images.Media._ID,
				MediaStore.Images.Media.DATA,
				MediaStore.Images.Media.DISPLAY_NAME,
				MediaStore.Images.Media.DATE_ADDED };

		Cursor cur = context.getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, null);

		while (cur.moveToNext()) {
			PhotoModel photo = new PhotoModel();
			int columnIndex = 0;

			columnIndex = cur
					.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
			if (columnIndex != -1) {
				photo.setTitle(cur.getString(columnIndex));
			}

			columnIndex = cur.getColumnIndex(MediaStore.Images.Media.DATA);
			if (columnIndex != -1) {
				photo.setPath(cur.getString(columnIndex));
			}

			columnIndex = cur.getColumnIndex(MediaStore.Images.Media._ID);
			if (columnIndex != -1) {
				photo.setId(cur.getLong(columnIndex));
			}

			columnIndex = cur
					.getColumnIndex(MediaStore.Images.Media.DATE_ADDED);
			if (columnIndex != -1) {
				photo.setDate(cur.getLong(columnIndex));
			}

			this.photos.add(photo);
		}

		this.isLoaded = true;
	}

	public ArrayList<PhotoModel> getPhotos() {
		return this.photos;
	}

	/*
	 * Lấy ArrayList chứa đường dẫn của tất cả hình ảnh đã load.
	 */
	public ArrayList<String> getImagesPath() {
		ArrayList<String> ret = new ArrayList<String>();
		for (PhotoModel photo : this.photos) {
			ret.add(photo.getPath());
		}
		return ret;
	}

	/*
	 * Load hình ảnh từ SdCard.
	 */
	public void loadPhotosFromExternalStorage() throws Exception {
		if (!ExternalStorageHelpers.externalStorageAvailable()) {
			throw new Exception();
		}

		this.photos.clear();
		this.isLoaded = false;
		this.load(android.os.Environment.getExternalStorageDirectory());
		this.isLoaded = true;
	}

	/*
	 * Load hình ảnh trong thư mục chỉ định. Phương thức đệ qui.
	 */
	private void load(File currentDir) {
		if (!currentDir.isDirectory())
			return;

		// Không hiển thị hình ảnh thumbnail tạo bởi hệ thống.
		if (currentDir.getName().equalsIgnoreCase(Contants.ThumbnailFolder))
			return;

		File[] files = currentDir.listFiles();

		for (File file : files) {
			if (file.isFile()) { // File.
				String path = file.getAbsolutePath();
				long lastModified = file.lastModified();

				if (!BitmapHelpers.isImage(path))
					continue;

				PhotoModel photo = new PhotoModel();

				photo.setPath(path);
				photo.setDate(lastModified);

				this.photos.add(photo);
			} else { // Directory.
				this.load(file);
			}
		}
	}

	public void deletePhoto(PhotoModel photo) {
		this.photos.remove(photo);
		ExternalStorageHelpers.deleteFile(photo.getPath());
	}

}